import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA
 * Description：阻塞队列实现生产者消费者模型
 * User：chenshu
 * Date：2023-11-08
 * Time：19:34
 */
public class Demo4 {
    public static void main(String[] args) {
        BlockingDeque<Integer> queue = new LinkedBlockingDeque();

        Thread Customer = new Thread(() -> {
            while (true) {
                try {
                    int n = queue.take();
                    System.out.println("消费者取数据：" + n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread Producer = new Thread(() -> {
            int n = 0;
            while(true) {
                try {
                    System.out.println("生产者存数据：" + n);
                    queue.put(n);
                    n++;
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Customer.start();
        Producer.start();
    }
}
